/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/17
 * Time:23:16
 */
public class Solution {
    //
    public static int search(int[] nums, int target) {
        int len = nums.length;
        int left = 0;
        int right = len - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            // 注意：这里要写 <=，因为 left 与 mid 有可能重合
            if (nums[left] <= nums[mid]) {
                // target 落在有序的部分，即 nums[left] <= target < nums[mid]
                if (nums[left] <= target && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            //nums[left] > nums[mid]
            } else {
                // target 落在有序的部分，即 nums[mid] < target  <= nums[right]
                if (nums[mid] < target && target <= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
    //
    public static void main(String[] args) {
        System.out.println(search(new int[]{4, 5, 6, 7, 0, 1, 2}, 3));
    }
}
